﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace System.Web.UI
{
    public class ZMPageVList<TViewModel, TMainModel> : ZMPageVList<BaseBLL<TViewModel>, BaseBLL<TMainModel>, TViewModel, TMainModel>
        where TViewModel : BaseModel, new()
        where TMainModel : BaseModel, new()
    { }
    public class ZMPageVList<TViewBLL, TMainBLL, TViewModel, TMainModel> : ZMPageList<TViewBLL, TViewModel>
        where TViewModel : BaseModel, new()
        where TMainModel : BaseModel, new()
        where TMainBLL : BaseBLL<TMainModel>, new()
        where TViewBLL : BaseBLL<TViewModel>, new()
    {

        protected override string DefaultSortField
        {
            get
            {
                if (string.IsNullOrEmpty(base.DefaultSortField))
                {
                    TMainModel mainModel = new TMainModel();
                    return mainModel.GetPrimaryKey();
                }
                return base.DefaultSortField;
            }
        }

        protected override void SetControlsProperty()
        {
            base.SetControlsProperty();
            if (BaseMainGrid.DataKeyNames == null || BaseMainGrid.DataKeyNames.Length <= 0 || string.IsNullOrEmpty(BaseMainGrid.DataKeyNames[0]))
            {
                TMainModel mainModel = new TMainModel();
                string primaryKey = mainModel.GetPrimaryKey();
                if (!string.IsNullOrEmpty(primaryKey))
                    BaseMainGrid.DataKeyNames = new string[] { primaryKey };
            }
        }

        protected override void OnMainGridRowDeleting(GridViewDeleteEventArgs e)
        {
            OnMainGridRowDeletingTMethod<TMainBLL, TMainModel>(e);
        }

        protected override void OnMianGridRowUpdating(GridViewUpdateEventArgs e)
        {
            OnMianGridRowUpdatingTMethod<TMainBLL, TMainModel>(e);
        }

        protected override void ToolBarDeleteClick(object sender, ImageClickEventArgs e)
        {   //base.ToolBarDeleteClick(sender, e);
            string str = "";
            foreach (string s in GetCheckedList())
            {
                str += "'" + s + "',";
            }
            str = str.Trim(',');
            if (!string.IsNullOrEmpty(str))
            {
                str = (new TMainModel()).GetPrimaryKey() + " IN (" + str + ")";
                TMainBLL bll = new TMainBLL();
                bll.Delete(str, new List<DBParameter>());
                OnGridChanged();
            }
        }

    }
}
